package com.csinc.generate.core.base.log;

import com.csinc.generate.core.base.model.LoginLogModel;
import com.csinc.generate.core.base.model.OperationLogModel;
import com.csinc.generate.core.common.state.LogSucceed;
import com.csinc.generate.core.common.state.LogType;
import com.csinc.generate.core.utils.HighFrequencyMethod;
import lombok.extern.slf4j.Slf4j;

import java.util.TimerTask;

/**
 * 日志操作任务创建工厂
 * Create By 郭金宝 on 2018/7/30.<br>
 */
@Slf4j
public class LogTaskFactory {

    public static TimerTask loginLog(final Integer userId, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    LoginLogModel loginLog = LogFactory.createLoginLog(LogType.LOGIN, userId, null, ip);
                    log.info("将登陆信息写入数据库等：具体登陆日志为："+loginLog.toString());
                } catch (Exception e) {
                    log.error("创建登陆日志异常!", e);
                }
            }
        };
    }

    public static TimerTask loginLog(final String username, final String msg, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                LoginLogModel loginLog = LogFactory.createLoginLog( LogType.LOGIN_FAIL, null, "账号:" + username + "," + msg, ip);
                try {
                    log.info("将登陆信息写入数据库等：具体登陆日志为："+loginLog.toString());
                } catch (Exception e) {
                    log.error("创建登陆失败异常!", e);
                }
            }
        };
    }

    public static TimerTask exitLog(final Integer userId, final String ip) {
        return new TimerTask() {
            @Override
            public void run() {
                LoginLogModel loginLog = LogFactory.createLoginLog(LogType.EXIT, userId, null, ip);
                try {
                    log.info("将退出信息写入数据库等：具体退出日志为："+loginLog.toString());
                } catch (Exception e) {
                    log.error("创建退出日志异常!", e);
                }
            }
        };
    }

    public static TimerTask bussinessLog(final Integer userId, final String bussinessName, final String clazzName, final String methodName, final String msg) {
        return new TimerTask() {
            @Override
            public void run() {
                OperationLogModel operationLog = LogFactory.createOperationLog(
                        LogType.BUSSINESS, userId, bussinessName, clazzName, methodName, msg, LogSucceed.SUCCESS);
                try {
                    log.info("将业务信息写入数据库等：具体业务日志为："+operationLog.toString());
                } catch (Exception e) {
                    log.error("创建业务日志异常!", e);
                }
            }
        };
    }

    public static TimerTask exceptionLog(final Integer userId, final Exception exception) {
        return new TimerTask() {
            @Override
            public void run() {
                String msg = HighFrequencyMethod.getExceptionMsg(exception);
                OperationLogModel operationLog = LogFactory.createOperationLog(
                        LogType.EXCEPTION, userId, "", null, null, msg, LogSucceed.FAIL);
                try {
                    log.info("将异常信息写入数据库等：具体异常日志为："+operationLog.toString());
                } catch (Exception e) {
                    log.error("创建异常日志异常!", e);
                }
            }
        };
    }
}
